/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ourgame.tankgame.test.ld;

import com.bulletphysics.collision.shapes.QuantizedBvhNodes;
import com.jme3.animation.LoopMode;
import com.jme3.asset.AssetManager;
import com.jme3.cinematic.MotionPath;
import com.jme3.cinematic.MotionPathListener;
import com.jme3.cinematic.events.MotionTrack;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

/**
 *
 * @author L
 */
public class MontionSys {

    public static MotionTrack generateMontion(Spatial spatial, Vector3f[] vecs, AssetManager assetManager, Node rootNode) {
        final MotionPath path = new MotionPath();

        for (Vector3f vector3f : vecs) {
            path.addWayPoint(vector3f);
        }
        
        path.setCycle(true);
//        path.enableDebugShape(assetManager, rootNode);
        MotionTrack motionControl = new MotionTrack(spatial, path);
        motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
        Quaternion q0 = new Quaternion();
        q0.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
        
        Quaternion q = new Quaternion(new float[]{0, 0, FastMath.PI});
//        motionControl.setRotation(new Quaternion().fromAngleNormalAxis(0, Vector3f.UNIT_X.negate()));
        motionControl.setRotation(q0);
        motionControl.setInitialDuration(10f);
        motionControl.setSpeed(1f);
        motionControl.setLoopMode(LoopMode.Loop);


//        path.addListener(new MotionPathListener() {
//
//            public void onWayPointReach(MotionTrack motionControl, int wayPointIndex) {
//                if (path.getNbWayPoints() - wayPointIndex == 1) {
//
//                    Quaternion q = new Quaternion(new float[]{0, FastMath.PI, 0});
//                    motionControl.setRotation(q);                    
//                    System.out.println("完成一圈");
//                } else if (wayPointIndex == 8) {
//                    Quaternion q = new Quaternion(new float[]{0, FastMath.PI, FastMath.PI});
//                    motionControl.setRotation(q);
//                } else {
//                    System.out.println("运行到:" + wayPointIndex);
//                }
//            }
//        });

        return motionControl;
    }

    /**
     * 测试
     * @param spatial
     * @deprecated 
     * @return 
     */
    public static MotionTrack generateMontion(Spatial spatial) {
        MotionPath path = new MotionPath();
        path.addWayPoint(new Vector3f(10, 3, 0));
        path.addWayPoint(new Vector3f(10, 3, 10));
        path.addWayPoint(new Vector3f(-40, 3, 10));
        path.addWayPoint(new Vector3f(-40, 3, 0));
        path.addWayPoint(new Vector3f(-40, 8, 0));
        path.addWayPoint(new Vector3f(10, 8, 0));
        path.addWayPoint(new Vector3f(10, 8, 10));
        path.addWayPoint(new Vector3f(15, 8, 10));
//        path.enableDebugShape(assetManager, rootNode);
        MotionTrack motionControl = new MotionTrack(spatial, path);
        motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
        motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
        motionControl.setInitialDuration(10f);
        motionControl.setSpeed(0.1f);
        return motionControl;
    }
}
